package org.dromara.module.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.module.domain.ProductOrderInfo;
import org.dromara.module.service.IProductOrderInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单详情内容  订单里面的 商品列
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/product/order/info")
public class ProductOrderInfoController extends BaseController {

    private final IProductOrderInfoService productOrderInfoService;

    /**
     * 查询订单详情内容  订单里面的 商品列列表
     */
    @SaCheckPermission("module:orderInfo:list")
    @GetMapping("/list")
    public TableDataInfo<ProductOrderInfo> list(ProductOrderInfo productOrderInfo, PageQuery pageQuery) {
        QueryWrapper<ProductOrderInfo> qw = new QueryWrapper<>();

        TableDataInfo<ProductOrderInfo> td = PageUtils.buildDataInfo(productOrderInfoService.page(PageUtils.buildPage(),qw));
        List<ProductOrderInfo> list = td.getRows();
        return td;
    }

    /**
     * 导出订单详情内容  订单里面的 商品列列表
     */
    @SaCheckPermission("module:orderInfo:export")
    @Log(title = "订单详情内容  订单里面的 商品列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductOrderInfo productOrderInfo, HttpServletResponse response) {
        List<ProductOrderInfo> list = productOrderInfoService.list();
        ExcelUtil.exportExcel(list, "订单详情内容  订单里面的 商品列", ProductOrderInfo.class, response);
    }

    /**
     * 获取订单详情内容  订单里面的 商品列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:orderInfo:query")
    @GetMapping("/{id}")
    public R<ProductOrderInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(productOrderInfoService.getById(id));
    }

    /**
     * 新增订单详情内容  订单里面的 商品列
     */
    @SaCheckPermission("module:orderInfo:add")
    @Log(title = "订单详情内容  订单里面的 商品列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductOrderInfo productOrderInfo) {
        return toAjax(productOrderInfoService.save(productOrderInfo));
    }

    /**
     * 修改订单详情内容  订单里面的 商品列
     */
    @SaCheckPermission("module:orderInfo:edit")
    @Log(title = "订单详情内容  订单里面的 商品列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductOrderInfo productOrderInfo) {
        return toAjax(productOrderInfoService.updateById(productOrderInfo));
    }

    /**
     * 删除订单详情内容  订单里面的 商品列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:orderInfo:remove")
    @Log(title = "订单详情内容  订单里面的 商品列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productOrderInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
